// pages/xueliu/xueliu.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showLoading:false,
    windowHeight:wx.getWindowInfo().windowHeight,
    tipsHeight:wx.getWindowInfo().windowHeight < wx.getWindowInfo().windowWidth ?  wx.getWindowInfo().windowHeight  * 0.4 :wx.getWindowInfo().windowWidth * 0.4,
    Majiang: [false, {
      name: "1筒",
      bg: [0, -294]
    }, {
        name: "2筒",
        bg: [-90, -294]
      }, {
        name: "3筒",
        bg: [-184, -294]
      }, {
        name: "4筒",
        bg: [-274, -294]
      }, {
        name: "5筒",
        bg: [-366, -294]
      }, {
        name: "6筒",
        bg: [-460, -294]
      }, {
        name: "7筒",
        bg: [-550, -294]
      }, {
        name: "8筒",
        bg: [-640, -294]
      }, {
        name: "9筒",
        bg: [-736, -294]
      }, false, {
        name: "1条",
        bg: [0, -158]
      }, {
        name: "2条",
        bg: [-90, -158]
      }, {
        name: "3条",
        bg: [-184, -158]
      }, {
        name: "4条",
        bg: [-274, -158]
      }, {
        name: "5条",
        bg: [-366, -158]
      }, {
        name: "6条",
        bg: [-460, -158]
      }, {
        name: "7条",
        bg: [-550, -158]
      }, {
        name: "8条",
        bg: [-640, -158]
      }, {
        name: "9条",
        bg: [-736, -158]
      }, false, {
        name: "1万",
        bg: [0, -25]
      }, {
        name: "2万",
        bg: [-90, -25]
      }, {
        name: "3万",
        bg: [-184, -25]
      }, {
        name: "4万",
        bg: [-274, -25]
      }, {
        name: "5万",
        bg: [-366, -25]
      }, {
        name: "6万",
        bg: [-460, -25]
      }, {
        name: "7万",
        bg: [-550, -25]
      }, {
        name: "8万",
        bg: [-640, -25]
      }, {
        name: "9万",
        bg: [-736, -25]
      }],
    AddPai: {
      tong: [1, 2, 3, 4, 5, 6, 7, 8, 9],
      tiao: [11, 12, 13, 14, 15, 16, 17, 18, 19],
      wan: [21, 22, 23, 24, 25, 26, 27, 28, 29]
    },
    Shoupai: [],
    addType: "1",
    tingPai: [{ msg: '暂未听牌', da: 0, ting: [] }]
  }, 
  chanageType: function (e: any) { 
    this.setData({
      addType: e.target.dataset.type
    })
  },

  clearshoupai: function (e: any) {
    this.setData({
      Shoupai: []
    })
    this.doit()
  },

  addShoupai: function (e: any) {
    let hasSoupais: number[] = this.data.Shoupai
    let nowChoosePai = e.target.dataset.pai
    if (hasSoupais.length < 14) {
      //判断出现牌的数量
      let chuxians = hasSoupais.filter((v) => v == nowChoosePai)
      if (chuxians.length >= 4) {
        wx.showToast({
          title: '超出',
          icon: 'error',
          duration: 1000
        })
        return
      }
      hasSoupais.push(nowChoosePai)
      hasSoupais.sort(function (a, b) {
        return a - b
      })
      this.setData({
        //@ts-ignore
        Shoupai: hasSoupais
      })
      this.doit()
    } else {
      wx.showToast({
        title: '超出',
        icon: 'error',
        duration: 1000
      })
    }
  },
  delShoupai: function (e: any) {
    let nowChoosePai = e.target.dataset.pai
    let hasSoupais: number[] = this.data.Shoupai
    let index = hasSoupais.findIndex((v => v == nowChoosePai))
    if (index > -1) {
      hasSoupais.splice(index, 1)
      this.setData({
        //@ts-ignore
        Shoupai: hasSoupais
      })
      this.doit()
    }
  },
  chating: function (arr: number[]): number[] {
    let shouPaiList = arr.slice()
    let tingPaiList: number[] = []
    //判断花色
    let hasTong = false
    let hasTiao = false
    let hasWan = false
    if (shouPaiList.findIndex((v) => this.data.AddPai.tiao.includes(v)) > -1) {
      hasTiao = true
    }
    if (shouPaiList.findIndex((v) => this.data.AddPai.wan.includes(v)) > -1) {
      hasWan = true
    }
    if (shouPaiList.findIndex((v) => this.data.AddPai.tong.includes(v)) > -1) {
      hasTong = true
    }
    if (hasTiao && hasWan && hasTong) {
      wx.showToast({
        title: '花猪',
        icon: 'error',
        duration: 1000
      })
      return []
    }
    let allPais = []
    if (hasWan) {
      allPais.push(...this.data.AddPai.wan)
    }
    if (hasTiao) {
      allPais.push(...this.data.AddPai.tiao)
    }
    if (hasTong) {
      allPais.push(...this.data.AddPai.tong)
    }
    allPais.forEach((v) => {
      let tempS = shouPaiList.slice()
      tempS.push(v)
      if (this.isHupai(tempS)) {
        tingPaiList.push(v)
      }
    })
    return tingPaiList
  },
  chaChunpai: function () {
    let shouPaiList: number[] = JSON.parse(JSON.stringify(this.data.Shoupai))
    let chupais: {
      msg: string;
      da: number;
      ting: number[];
    }[] = []
    let shouUnique = [...new Set(shouPaiList)];
    shouUnique.forEach((v, i) => {
      let temp = shouPaiList.slice()
      temp.splice(temp.findIndex((x=>x ==v)), 1)
      let tingList: number[] = this.chating(temp)
      if (tingList.length > 0) {
        chupais.push({ msg: '', da: v, ting: tingList })
      }
    });
    //排序
    chupais.sort(function (a, b) {
        return   b.ting.length - a.ting.length
      })

    this.setData({
      //@ts-ignore
      tingPai: chupais
    })
  },

  doit: function () {
    this.setData({
      showLoading:true,
    })
    let nums = this.data.Shoupai.length
    let listShoupai = JSON.parse(JSON.stringify(this.data.Shoupai))
    if (nums % 3 == 1) {
      let temp  =  this.chating(listShoupai)
      this.setData({
        //@ts-ignore
        tingPai: [{ msg: temp.length>0?'听':'暂未听牌', da: 0, ting:temp }]
      })
    } else if (nums % 3 == 2) {
      this.chaChunpai()
    } else {
      this.setData({
        //@ts-ignore
        tingPai: [{ msg: '小相公', da: 0, ting:[] }]
      })
    }
    this.setData({
      showLoading:false,
    })
  },

  isHupai: function (PaiList: number[]): boolean {
    //排序
    PaiList.sort(function (a, b) {
      return a - b
    })
    //取出所有的将牌
    let jiangs = this.getJiangPai(PaiList)
    if (jiangs.length == 0) {
      return false
    }
    //去除将牌
    let otherPais = this.removeJiang(jiangs, PaiList)
    //单调
    if (jiangs.length == 1 && otherPais[0].length == 0) {
      return true
    }
    //打散为3个一组
    let reBool = false
    otherPais.forEach(v => {
      if (this.get3pai(v)) {
        reBool = true
      }
    });
    return reBool
  },

  //返回所有可以作为将的牌
  getJiangPai: function (PaiList: number[]): number[] {
    let re: number[] = []
    PaiList.forEach(v => {
      let tems = PaiList.filter((item) => item == v)
      if (tems.length >= 2) {
        re.push(v)
      }
    });
    //去除重复  
    return [...new Set(re)];
  },
  //去除将牌
  removeJiang: function (jiangs: number[], PaiList: number[]): Array<Array<number>> {

    let re: Array<Array<number>> = []
    jiangs.forEach(jiang => {
      //去除两次
      let _PaiList: number[] = JSON.parse(JSON.stringify(PaiList))
      let index = _PaiList.findIndex(v => v == jiang)
      _PaiList.splice(index, 1)
      index = _PaiList.findIndex(v => v == jiang)
      _PaiList.splice(index, 1)
      re.push(_PaiList)
    });
    return re
  },

  //遍历取出3个,并判断是否好的牌
  get3pai: function (PaiList: number[]): boolean {
    let len_pai = PaiList.length
    if (len_pai % 3 != 0) {
      return false
    }
    //取出3个 
    let newPaiList = PaiList.slice()
    //取出所有3个排序
    function getArrAllSort(originArr: number[]): Array<Array<number>> {
      let reWarp: Array<Array<number>> = []
      let tem = originArr.slice()
      let len1 = tem.length
      for (let index1 = 0; index1 < len1; index1++) {
        let now1 = tem[index1]
        let tem2 = tem.slice()
        tem2.splice(index1, 1)
        let len2 = tem2.length
        for (let index2 = 0; index2 < len2; index2++) {
          let now2 = tem2[index2]
          let tem3 = tem2.slice()
          tem3.splice(index2, 1)
          let len3 = tem3.length
          for (let index3 = 0; index3 < len3; index3++) {
            let now3 = tem3[index3]
            if (index3 >= index2 && index2 >= index1) {
              reWarp.push([now1, now2, now3])
            }
          }
        }
      }
      return reWarp
    }

    // 是否符合正确的牌
    function isOk(arr: number[]): boolean {
      return (arr[0] == arr[1] && arr[1] == arr[2]) || (arr[0] + 1 == arr[1] && arr[1] + 1 == arr[2])
    }

    //所有牌是否ok

    let boolTem = false
    function perOkpai(arrPais: number[]) {
      let break1 = getArrAllSort(arrPais)
      break1.forEach(v1 => {
        if (isOk(v1)) {
          //判断剩下的
          let shengxiaArr = arrPais.slice()
          v1.forEach((v1_item) => {
            let tem_index = shengxiaArr.findIndex((sx_item) => sx_item == v1_item)
            shengxiaArr.splice(tem_index, 1)
          })

          if (shengxiaArr.length > 0) {
            perOkpai(shengxiaArr)
          } else {
            boolTem = true
          }
        }
      });

    }
    perOkpai(newPaiList)
    return boolTem
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { 
    return {
      title: '血流麻将胡牌计算器，再也不担心我炸糊了！',
      path: '/pages/xueliu/xueliu', 
    }
  }
})